package com.zz.ecommerce.togatherDeclareMgr.custDeclarationUpload;

import com.zz.ecommerce.togatherDeclareMgr.custDeclarationUpload.service.DecMessageService;
import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

/**
 * 获取报文controller
 */
@Controller
@RequestMapping("/message")
public class MessageController {

    private final Logger log = LoggerFactory.getLogger(getClass());

    @Autowired
    private DecMessageService decMessageService;

    @RequestMapping(value = "/getDecMessageJson/{conIndx}/{promiseItmes}", produces = "text/html;charset=UTF-8",
                    method =
                        {RequestMethod.POST, RequestMethod.GET})
    @ResponseBody
    public String getDecMessageJson(@PathVariable("conIndx") String colIndx, @PathVariable("promiseItmes") String
        promiseItmes) {
        //        if(StringUtils.isEmpty(decMessageService.getPassCode(colIndx))
        //        {
        //            throw new RuntimeException("通关单号为空!");
        //        }
        String jsonMsg = decMessageService.getDecMesseageJson(colIndx, promiseItmes);

        if (StringUtils.isNotEmpty(jsonMsg)) {
            return StringUtils.deleteWhitespace(jsonMsg.replaceAll("\\r\\n", "").replaceAll("\\t", ""));
        } else {
            throw new RuntimeException("生成json报文异常.");
        }
    }

    @RequestMapping("/updateDeclareCustSuccessStatus")
    @ResponseBody
    public boolean updateDeclareCustSuccessStatus(@RequestParam(value = "conIndx") String conIndx) {
        final String successStatus = "1";
        return decMessageService.updateDeclareCustStatus(conIndx, successStatus);
    }
}
